import * as React from 'react'
import {Component} from 'react'
import {StyleSheet, View, YellowBox} from 'react-native';
import getRouter from "./router";
import Store from "./store";
import {Provider} from "mobx-react";
import CustomToast from "./component/common/CustomToast";
import * as wechat from 'react-native-wechat'
import {afterAnimation} from "./util";
import initializeConfig from "./config/PlatformConfig";
import CodePush, {DownloadProgress} from "react-native-code-push";
import {deploymentKey, dev, wechatAppId} from "./config";
import toast from "./component/common/ToastProxy";
import ConfigStoreInstance from "./store/modules/config";


initializeConfig();
export default class App extends Component {
    toast: CustomToast = new CustomToast();
    loadingToggle: boolean = true;
    showSuccess = (text: string, duration?: number, callback?: () => void) => {
        this.toast.showSuccess(text, duration, callback);
    };
    showError = (text: string, duration?: number, callback?: () => void) => {
        this.toast.showError(text, duration, callback);
    };
    showLoading = (text: string = "加载中...") => {
        this.toast.showLoading(text);
    };
    hideLoading = () => {
        this.toast.hideLoading();
    };


    codePushStatusDidChange = (syncStatus) => {
        if (syncStatus === CodePush.SyncStatus.UNKNOWN_ERROR) {
            global.showError('更新出错，请重启应用')

        }
    };
    codePushDownloadDidProgress = (progress: DownloadProgress) => {
        const currProgress = progress.receivedBytes / progress.totalBytes;
        const flag = currProgress >= 1;
        //只有当状态发生变化时才调用
        if (flag !== this.loadingToggle) {
            if (flag) {
                global.hideLoading();
                toast("更新成功")
            } else {
                global.showLoading("更新中...")
            }
        }
        this.loadingToggle = flag;
    };

    componentDidMount() {
        afterAnimation(async () => {
            if (!dev) {
                await CodePush.sync({
                    deploymentKey,
                    updateDialog: {
                        optionalIgnoreButtonLabel: '稍后',
                        optionalInstallButtonLabel: '后台更新',
                        optionalUpdateMessage: '有新版本了，是否更新？',
                        mandatoryUpdateMessage: "有新版本了，是否更新？",
                        mandatoryContinueButtonLabel: "接收更新",
                        title: '更新提示'
                    },
                    installMode: CodePush.InstallMode.IMMEDIATE
                }, this.codePushStatusDidChange, this.codePushDownloadDidProgress);
            }
            await wechat.registerApp(wechatAppId);
            ConfigStoreInstance.checkInstalledWeChat();

        });
    }

    constructor(props) {
        super(props);
        global.showSuccess = this.showSuccess.bind(this);
        global.showError = this.showError.bind(this);
        global.showLoading = this.showLoading.bind(this);
        global.hideLoading = this.hideLoading.bind(this);
    }

    render() {

        return (
            <View style={styles.container}>
                <Provider {...Store}>
                    {getRouter()}
                </Provider>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#F5FCFF',
    }

});
